const Fetcher = require('./fetcher');
const CsvWriter = require('./csv-writer');

let headers = [];

async function fetchOnce(fetcher, writer, offset) {
  const logs = await fetcher.getLog(offset);
  let i = 0;
  while (true) {
    const item = logs[i];
    if (!item) break;
    if (headers.length === 0) {
      headers = Object.keys(item);
      writer.writeRow(...headers);
    }
    writer.writeRow(...headers.map((h) => item[h]));
    ++i;
  }
  return i;
}

async function main() {
  const fetcher = new Fetcher();
  const writer = new CsvWriter('./log.csv');
  writer.writeUtf8Bom(); // 为了excel和numbers都支持，需要写入BOM头
  let offset = 0;
  while (true) {
    const lines = await fetchOnce(fetcher, writer, offset);
    console.log('write offset, %d, lines, %d', offset, lines);
    if (lines === 0) break;
    offset += lines;
  }
  console.log("there's no more lines");
  writer.end();
}

main();
